var vm=new Vue({
    el:"#knowledge",
    data:{
        List:[],//查询结果集
        pageSize:5,//每页显示数量
        knowledge:{},//查询条件集
        Knowledge:{},//根据id查到的结果
        addKnowledge:{},//添加知识绑定的对象
        selectIds:[]//选中的ID集合
    },
    mounted(){
        //初始化页面加载的方法
        this.selectPage(1);
        this.getPageData();
    },
    methods:{
        //条件查询并分页
        selectPage:function (pageNum){
            let that=this;
            axios.post("/knowledge/selectPage.do?pageNum="+pageNum+"&pageSize="+this.pageSize,this.knowledge).then(function (response){
                that.List=response.data;
            })
        },
        //根据id删除
        deleteById:function (id){
            let that=this;
            axios.get("/knowledge/deleteById.do?ids="+id).then(function (response){
                that.selectPage(1);
            })
        },
        //更新复选
        updateSelection:function($event, id) {
            if($event.target.checked){//如果是被选中,则增加到数组
                this.selectIds.push(id);
            }else{
                let idx = this.selectIds.indexOf(id);//获取当前选中的id在数组中的下标
                this.selectIds.splice(idx, 1);//删除
            }
        },
        //根据id批量删除
        deleteByIds:function (){
            let that=this;
            axios.get("/knowledge/deleteById.do?ids="+this.selectIds).then(function (response){
                that.selectPage(1);
            })
            this.selectIds=[];
        },
        //根据id查询
        selectById:function (id){
            let that=this;
            axios.get("/knowledge/selectById.do?id="+id).then(function (response){
                that.Knowledge=response.data;
                localStorage.setItem('Knowledge',JSON.stringify(that.Knowledge));//将查询的结果存入本地缓存
            })
        },
        //修改
        update:function (){
            let that=this;
            axios.post("/knowledge/update.do",this.Knowledge).then(function (response){
                if (response.data.success){
                    localStorage.removeItem('Knowledge');//清除本地缓存中的Knowledge
                    alert(response.data.message)
                }else {
                    alert(response.data.message)
                }
            })
        },
        //刷新页面或者从别的页面进入，提取this.Knowledge的值
        getPageData:function (){
            this.Knowledge=JSON.parse(localStorage.getItem('Knowledge'));
            if (this.Knowledge==null){
                this.Knowledge={};
            }
        },
        //新增
        insert:function (){
            let  that=this;
            axios.post("/knowledge/insert.do",this.addKnowledge).then(function (response){
                if (response.data.success){
                    alert(response.data.message)
                }else {
                    alert(response.data.message)
                }
            })
        }

    }
})